package com.hjq.fcloud.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.bean.FileItemBean
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.utils.FileOpenUtils
import com.zhy.adapter.abslistview.CommonAdapter
import com.zhy.adapter.abslistview.ViewHolder
import kotlinx.android.synthetic.main.layout_pdf.view.listView
import kotlinx.android.synthetic.main.layout_pdf.view.pdfTitleTv


/**
 * 文件查看 view
 */
class FileView : RelativeLayout {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        inflateView(attrs)
    }


    private fun inflateView(attrs: AttributeSet?) {
        inflate(context, R.layout.layout_pdf, this)
    }

    fun setViewData(activity: AppActivity, title: String?, list: List<FileItemBean>?) {
        if(list != null && list.size > 0){
            setVisibility(true)
            pdfTitleTv.setText(title)
            listView.adapter = object : CommonAdapter<FileItemBean>(activity, R.layout.item_pdf, list) {
                override fun convert(viewHolder: ViewHolder, item: FileItemBean, position: Int) {
                    viewHolder.getView<TextView>(R.id.pdfNameTv).setText(item.name)
                    viewHolder.getView<View>(R.id.pdfJump)
                        .setOnClickListener {
//                        CommonPdfViewActivity.start(context, item.url)
                            FileOpenUtils.openBySystem(activity, item.url)
//                        openBySystem(activity,"https://ims.e-troin.com.cn/minio/minio/zh-work-order-know/%E5%90%88%E8%A7%84%E6%89%BF%E8%AF%BA-=1706776745382.pdf")
                        }
                }
            }
        }else{
            setVisibility(false)
        }
    }
}
